const express = require('express');
const router = express.Router();
const model = require('../models/models');
var User=model.user;
var fs = require("fs");
var host = "ec2-52-41-0-146.us-west-2.compute.amazonaws.com";
var port = 8082;
const imageutil = require('./imageUtil.js');
var multer = require('multer');
var upload1 = multer({ dest: 'uploads/' })

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'my-uploads')
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
});
var upload = multer({ storage: storage });

//tested done
router.post('/profile', upload.single('avatar'), function (req, res, next) {
    var a = req.file;
    var b = req.body;
    // req.file is the `avatar` file
    // req.body will hold the text fields, if there were any
    console.log('body:::' + JSON.stringify(req.body));
    //console.log(JSON.stringify(req.body.name));
    //var n = req.body.name;
    //console.log(n);
    res.send('profile');
})

//not tested yet
router.post('/photos/upload', upload.array('photos',10 ), function (req, res, next) {
    var a = req.files[0];
    var a1 = req.files[1];
    var b = req.body;
    console.log('originname:::' + a.originalname + ', ' + a1.originalname);
    // req.files is array of `photos` files
    // req.body will contain the text fields, if there were any
    res.send('photosUpload');
})

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
router.post('/cool-profile', cpUpload, function (req, res, next) {
    // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
    //
    // e.g.
    //  req.files['avatar'][0] -> File
    //  req.files['gallery'] -> Array
    var a = req.files['avatar'][0];
    var c = req.files['gallery'][0];
    var b = req.body;
    //
    // req.body will contain the text fields, if there were any
    res.json('cool profile');
})
module.exports = router;